package com.tg.app.activity.device.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;

import androidx.core.util.Consumer;

import com.appbase.custom.base.SettingData;
import com.appbase.custom.constant.NightVisionModeConstants;
import com.tange.base.toolkit.TGThreadPool;
import com.tange.core.device.facade.ConnectStatus;
import com.tange.core.device.facade.DeviceFacade;
import com.tange.core.device.facade.Status;
import com.tg.app.R;
import com.tg.app.TGDevice;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo;

import java.util.ArrayList;

public class NightVisionActivity extends DeviceSettingsBaseActivity{
    private final static String TAG = "NightVisionActivity";
    private final ArrayList<SettingData> listData = new ArrayList<>();
    private SettingAdapter adapter ;

    private DeviceFacade deviceFacade;
    private DeviceSettingsInfo info;
    private int clickPosition;
    private int selectMode;
    private int oldMode;

    @Override
    protected void onSetupFailed() {
        setPositionByMode(oldMode);
    }

    private void setPositionByMode(int oldMode) {
        if (oldMode == NightVisionModeConstants.NIGHT_VISION_MODE_AUTO){
            clickPosition = 0;
        }else if (oldMode== NightVisionModeConstants.NIGHT_VISION_MODE_DAY) {
            clickPosition = 2;
        } else if (oldMode == NightVisionModeConstants.NIGHT_VISION_MODE_NIGHT){
            clickPosition = 1;
        }
        setFlag(clickPosition);
    }

    private final Consumer<ConnectStatus> connectStatusConsumer = connectStatus -> {
        if (connectStatus.getStatus() == Status.SUCCESS){
            TGLog.d(TAG,"连接成功");
        }else if (connectStatus.getStatus() == Status.FAILED){
            TGLog.d(TAG,"连接失败");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deviceFacade.unObserveConnectStatus(connectStatusConsumer);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        setContentView(R.layout.activity_night_vision);
        initView();
        Intent intent = getIntent();
        info  = intent.getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        deviceFacade = new DeviceFacade(info.uuid);
        deviceFacade.observeConnectStatus(connectStatusConsumer);
        deviceFacade.connect();

        initData();
        backClickEvent();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
        modifyToolBar(getString(R.string.settings_night_mode));
        adapter = new SettingAdapter(listData,this);
        adapter.setFirstNormal(true);
        adapter.setCustomText(true);
        adapter.setItemTextSize(14.0f);
        adapter.setMarkTextSize(10.0f);
        ListView listView = findViewById(R.id.night_vision_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (!TGDevice.getInstance().isNetwork(deviceFacade)){
                if (!isFinishing() && !isDestroyed()) {
                    TGToast.showToast(R.string.txt_network_anomaly);
                }
            }else{
                if (position < listData.size()){
                    clickPosition = position;
                    switch(position){
                        case 2:
                            setNightVision(NightVisionModeConstants.NIGHT_VISION_MODE_DAY);
                            break;
                        case 1:
                            setNightVision(NightVisionModeConstants.NIGHT_VISION_MODE_NIGHT);
                            break;
                        case 0:
                            setNightVision(NightVisionModeConstants.NIGHT_VISION_MODE_AUTO);
                            break;
                    }

                    setFlag(clickPosition);
                }
            }
        });
    }

    private void initData(){
        int type = info.nightVision;
        SettingData auto = new SettingData(getString(R.string.settings_auto_switch_night_mode), getString(R.string.txt_night_mode_auto_open), 0);
        SettingData night = new SettingData(getString(R.string.open_alltime), "", 0);
        SettingData day = new SettingData(getString(R.string.always_off), "", 0);
        listData.add(auto);
        listData.add(night);
        listData.add(day);

        selectMode = type;
        oldMode = type;
        setPositionByMode(type);
    }

    private void setNightVision(int type){
        if (deviceFacade !=null){
            oldMode = selectMode;
            selectMode = type;
            deviceFacade.getInstruct().create(AVIOCTRLDEFs.IOTYPE_USEREX_IPCAM_SET_DAYNIGHT_REQ).data(AVIOCTRLDEFs.SMsgAVIoctrlSetDoubleLightReq.parseContent(type)).send(resp -> {
                if (resp.getSuccess()){
                    TGThreadPool.executeOnUiThread(() -> {
                        completeSend();
                        info.nightVision = selectMode;
                        sendUpdateSettingBroadcast(info);
                    });
                }else {
                    TGLog.d(TAG,"setRotate failed");
                }
            });
        }
    }

    private void setFlag(int position){
        for(int i = 0; i< listData.size(); i++){
            if(i == position){
                listData.get(i).setFlag(1);
            }else{
                listData.get(i).setFlag(0);
            }
        }
        adapter.notifyDataSetChanged();
    }
}
